/*===========================================================================
  Copyright (C) 2012-2014 by the Okapi Framework contributors
-----------------------------------------------------------------------------
  This library is free software; you can redistribute it and/or modify it 
  under the terms of the GNU Lesser General Public License as published by 
  the Free Software Foundation; either version 2.1 of the License, or (at 
  your option) any later version.

  This library is distributed in the hope that it will be useful, but 
  WITHOUT ANY WARRANTY; without even the implied warranty of 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License 
  along with this library; if not, write to the Free Software Foundation, 
  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

  See also the full LGPL text here: http://www.gnu.org/copyleft/lesser.html
===========================================================================*/

package net.sf.okapi.lib.xliff2.core;

import net.sf.okapi.lib.xliff2.Util;

/**
 * Represents a list of {@link Note} objects.
 */
public class Notes extends BaseList<Note> implements IWithExtAttributes {

	private ExtAttributes xattrs;

	/**
	 * Creates an empty {@link Notes} object.
	 */
	public Notes () {
		// Nothing to do
	}
	
	/**
	 * Copy constructor.
	 * @param original the original object to duplicate.
	 */
	public Notes (Notes original) {
		super(original);
		if ( original.hasExtAttribute() ) {
			xattrs = new ExtAttributes(original.xattrs);
		}
	}
	
	@Override
	public void setExtAttributes (ExtAttributes attributes) {
		this.xattrs = attributes;
	}

	@Override
	public ExtAttributes getExtAttributes () {
		if ( xattrs == null ) {
			xattrs = new ExtAttributes();
		}
		return xattrs;
	}

	@Override
	public boolean hasExtAttribute () {
		if ( xattrs == null ) return false;
		return !xattrs.isEmpty();
	}

	@Override
	public String getExtAttributeValue (String namespaceURI,
		String localName)
	{
		if ( xattrs == null ) return null;
		return xattrs.getAttributeValue(namespaceURI, localName);
	}

	/**
	 * Gets the note for a given fragment identifier or identifier in this object.
	 * @param fragIdOrId the fragment identifier (e.g. "#n=n1") or identifier (e.g. "n1").
	 * Allowing a fragment identifier is for convenience only: the full fragment may not be validated.
	 * @return the corresponding {@link Note} or null.
	 */
	public Note get (String fragIdOrId) {
		if ( Util.isNoE(fragIdOrId) ) return null;
		if ( isEmpty() ) return null;
		// Else: try to get the note
		int p = fragIdOrId.lastIndexOf('=');
		String id = null;
		if ( p > -1 ) {
			// We assume the whole fragId is valid (i.e. it points to the container)
			id = fragIdOrId.substring(p+1);
		}
		else id = fragIdOrId;
		for ( Note note : this ) {
			if ( note.getId().equals(id) ) return note;
		}
		// Id not found
		return null;
	}
}
